//
//  NSURL+extension.swift
//  iUtil
//
//  Created by wendachuan on 19/3/22.
//  Copyright © 2019年 wendachuan. All rights reserved.
//

import UIKit

public extension URL {

    /// 从字符串构造NSURL对象
    ///
    /// - parameter URLString: url字符串
    /// - returns: 构造成功则返回NSURL对象，否则返回nil
    static func validURLFromString(string URLString: String) -> URL? {
        var url = URL(string: URLString)
        if url == nil {
            if let escapeUrl = URLString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlFragmentAllowed) {
                url = URL(string: escapeUrl)
            }
        }
        return url
    }
}
